package de.eridea.learning.usermanagement.webinterface;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Locale;

public class UserBean implements Serializable {
	private String loginName;
	private String displayName;
	private Collection<String> groups;
	private boolean loggedIn = false;

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public Collection<String> getGroups() {
		return groups;
	}

	public void setGroups(Collection<String> groups) {
		this.groups = groups;
	}

	public boolean isLoggedIn() {
		return loggedIn;
	}

	public void setLoggedIn(boolean loggedIn) {
		if (loggedIn == false) {
			for (Field field : getClass().getDeclaredFields()) {
				if (!field.getName().equals("loggedIn")) {
					String setterName = "set"
							+ field.getName().substring(0, 1)
									.toUpperCase(Locale.ENGLISH)
							+ field.getName().substring(1);
					try {
						getClass().getMethod(setterName,
								new Class[] { field.getDeclaringClass() })
								.invoke(this, new Object[] { null });
					} catch (SecurityException e) {
						throw new RuntimeException(e.getMessage(), e);
					} catch (NoSuchMethodException e) {

					} catch (IllegalArgumentException e) {
						throw new RuntimeException(e.getMessage(), e);
					} catch (IllegalAccessException e) {
						throw new RuntimeException(e.getMessage(), e);
					} catch (InvocationTargetException e) {
						throw new RuntimeException(e.getMessage(), e);
					}
				}
			}
		}
		this.loggedIn = loggedIn;
	}
}
